const Base = require('./base.js');

module.exports = class extends Base {
  /**
  * index action
  * @return {Promise} []
  */
  async getbrandinfoAction() {
    const brandid = this.post('brandid');
    const shopid = this.post('shopid');

    const info = await this.model().query(' SELECT ob.name as brandname,of.name as factoryname FROM `think_or_brand` ob LEFT JOIN `think_or_factory` of ON ob.belongs = of.id WHERE ob.id = ' + brandid + ' ');
    const mypath = think.config('mypath');
    const getapi = this.controller(mypath + '/database');
    const goodsnum = await getapi.getbrandinfo(brandid, shopid);// 记录数量
    const subjectnum = await getapi.getbrandsubjectnum(brandid, shopid);// 记录数量

    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info, 'goodsnum': goodsnum, 'subjectnum': subjectnum});
  }
  // 品牌下的产品列表
  async getbrandgoodslistAction() {
    const brandid = this.post('brandid');
    const shopid = this.post('shopid');

    const info = await this.model().query(' SELECT g.* FROM `think_or_shop_goods` sg left join `think_or_goods` g on sg.goodsid = g.id WHERE sg.shopid = ' + shopid + ' and g.brandid = ' + brandid);
    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }

  // 品牌下的项目列表
  async getbrandsubjectlistAction() {
    const brandid = this.post('brandid');
    const shopid = this.post('shopid');

    const info = await this.model().query(' SELECT s.* FROM `think_or_shop_subject` os left join `think_or_subject` s on os.subjectid = s.id WHERE os.shopid = ' + shopid + ' and s.brandid = ' + brandid);
    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }

  // 品牌普及客户
  async getbrandpujilistAction() {
    const shopid = this.post('shopid');
    const brandid = this.post('brandid');
    const start_time = this.post('start_time_js');
    const end_time = this.post('end_time_js');

    const info = await this.model().query('SELECT og.id,og.name,og.headurl,og.lv FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id LEFT JOIN `think_or_guest` og ON oso.guestid = og.id WHERE oso.addtime >= ' + start_time + ' AND oso.addtime < ' + end_time + ' AND osoi.departmentid = ' + shopid + ' AND osoi.brandid = ' + brandid + ' GROUP BY osoi.brandid,oso.guestid');

    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }

  // 品牌未普及客户
  async getbrandnopujilistAction() {
    const shopid = this.post('shopid');
    const brandid = this.post('brandid');
    const start_time = this.post('start_time_js');
    const end_time = this.post('end_time_js');
    const info = await this.model().query(' SELECT * FROM `think_or_guest` WHERE departmentid = ' + shopid + ' AND id NOT IN (SELECT oso.guestid FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id  WHERE oso.addtime >= ' + start_time + ' AND oso.addtime < ' + end_time + ' AND osoi.departmentid = ' + shopid + ' AND osoi.brandid = ' + brandid + ' GROUP BY osoi.brandid,oso.guestid) ');
    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }

  // 获取卡片信息
  async getcardinfoAction() {
    const cardid = this.post('cardid');
    const info = await this.model().query(' SELECT  * FROM `think_or_shop_card` WHERE id = ' + cardid + ' ');

    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }

  // 获取卡片普及客户信息
  async getcardpujilistAction() {
    const shopid = this.post('shopid');
    const cardid = this.post('cardid');
    const start_time = this.post('start_time_js');
    const end_time = this.post('end_time_js');

    const info = await this.model().query(' SELECT og.name,og.id,og.headurl,og.lv FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id LEFT JOIN `think_or_guest` og ON og.id = oso.guestid WHERE oso.addtime >= ' + start_time + ' AND oso.addtime < ' + end_time + ' AND osoi.departmentid = ' + shopid + ' AND osoi.type = 3 AND osoi.typeid = ' + cardid + ' GROUP BY oso.guestid  ');

    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }

  // 卡类未普及客户
  async getcardnopujilistAction() {
    const shopid = this.post('shopid');
    const cardid = this.post('cardid');
    const start_time = this.post('start_time_js');
    const end_time = this.post('end_time_js');

    const info = await this.model().query(' SELECT * FROM `think_or_guest` WHERE departmentid = ' + shopid + ' AND id NOT IN (SELECT oso.guestid FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id WHERE oso.addtime >= ' + start_time + ' AND oso.addtime < ' + end_time + ' AND osoi.departmentid = ' + shopid + ' AND osoi.type = 3 AND osoi.typeid = ' + cardid + ' GROUP BY oso.guestid  ) ');

    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }

  // 获取项目信息
  async getsubjectinfoAction() {
    const subjectid = this.post('subjectid');

    const info = await this.model().query(' SELECT  os.name as subjectname,ob.name as brandname FROM `think_or_subject` os LEFT JOIN `think_or_brand` ob ON os.brandid = ob.id WHERE os.id = ' + subjectid + ' ');

    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }

  // 获取项目的普及客户
  async getsubjectpujilistAction() {
    const shopid = this.post('shopid');
    const subjectid = this.post('subjectid');
    const start_time = this.post('start_time_js');
    const end_time = this.post('end_time_js');

    const info = await this.model().query(' SELECT og.name,og.id,og.headurl,og.lv FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id LEFT JOIN `think_or_guest` og ON og.id = oso.guestid WHERE oso.addtime >= ' + start_time + ' AND oso.addtime < ' + end_time + ' AND osoi.departmentid = ' + shopid + ' AND osoi.type = 2 AND osoi.typeid = ' + subjectid + ' GROUP BY oso.guestid  ');

    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }

  // 获取项目未普及客户
  async getsubjectnopujilistAction() {
    const shopid = this.post('shopid');
    const subjectid = this.post('subjectid');
    const start_time = this.post('start_time_js');
    const end_time = this.post('end_time_js');

    const info = await this.model().query(' SELECT * FROM `think_or_guest` WHERE departmentid = ' + shopid + ' AND id NOT IN (SELECT oso.guestid FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id WHERE oso.addtime >= ' + start_time + ' AND oso.addtime < ' + end_time + ' AND osoi.departmentid = ' + shopid + ' AND osoi.type = 2 AND osoi.typeid = ' + subjectid + ' GROUP BY oso.guestid  ) ');

    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }

  // 获取家居信息
  async getgoodsinfoAction() {
    const goodsid = this.post('goodsid');
    const info = await this.model().query(' SELECT og.name as goodsname,ob.name as brandname FROM `think_or_goods` og LEFT JOIN `think_or_brand` ob ON og.brandid = ob.id WHERE og.id = ' + goodsid + ' ');

    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }

  // 获取家居普及客户
  async getgoodspujilistAction() {
    const shopid = this.post('shopid');
    const goodsid = this.post('goodsid');
    const start_time = this.post('start_time_js');
    const end_time = this.post('end_time_js');

    const info = await this.model().query(' SELECT og.name,og.id,og.headurl,og.lv FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id LEFT JOIN `think_or_guest` og ON og.id = oso.guestid WHERE oso.addtime >= ' + start_time + ' AND oso.addtime < ' + end_time + ' AND osoi.departmentid = ' + shopid + ' AND osoi.type = 1 AND osoi.typeid = ' + goodsid + ' GROUP BY oso.guestid  ');

    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }

  // 获取家居未普及客户
  async getgoodsnopujilistAction() {
    const shopid = this.post('shopid');
    const goodsid = this.post('goodsid');
    const start_time = this.post('start_time_js');
    const end_time = this.post('end_time_js');

    const info = await this.model().query(' SELECT * FROM `think_or_guest` WHERE departmentid = ' + shopid + ' AND id NOT IN (SELECT oso.guestid FROM `think_or_shop_orderinfo` osoi LEFT JOIN `think_or_shop_order` oso ON osoi.orderid = oso.id WHERE oso.addtime >= ' + start_time + ' AND oso.addtime < ' + end_time + ' AND osoi.departmentid = ' + shopid + ' AND osoi.type = 1 AND osoi.typeid = ' + goodsid + ' GROUP BY oso.guestid  ) ');

    return think.isEmpty(info) ? this.json({'code': '200', 'msg': '没有数据...', 'data': ''}) : this.json({'code': '100', 'msg': '查询成功...', 'data': info});
  }
};
